package com.gitee.melin.bee.core.jdbc.dialect.starrocks;

import com.gitee.melin.bee.core.jdbc.dialect.AbstractDBQuery;

import java.sql.ResultSet;
import java.sql.SQLException;

/** StarRocksQuery */
public class StarRocksQuery extends AbstractDBQuery {

    String QUERY_TABLE_BY_SCHEMA =
            " select TABLE_NAME AS `NAME`,TABLE_SCHEMA AS `SCHEMA`,TABLE_COMMENT AS COMMENT, '' as TYPE, '' as CATALOG, "
                    + "'' as ENGINE , '' as OPTIONS , 0 as `ROWS`, null as CREATE_TIME, null as UPDATE_TIME from information_schema.tables where TABLE_SCHEMA = '%s'  ";

    /** 查询指定schema.table下的所有列信息 */
    String QUERY_COLUMNS_BY_TABLE_AND_SCHEMA = "  show full columns from `%s`.`%s` ";

    @Override
    public String tablesSql(String schemaName) {
        return String.format(QUERY_TABLE_BY_SCHEMA, schemaName);
    }

    @Override
    public String columnsSql(String schemaName, String tableName) {
        return String.format(QUERY_COLUMNS_BY_TABLE_AND_SCHEMA, schemaName, tableName);
    }

    @Override
    public String schemaName() {
        return "Database";
    }

    @Override
    public String tableName() {
        return "NAME";
    }

    @Override
    public String tableComment() {
        return "COMMENT";
    }

    @Override
    public String columnName() {
        return "Field";
    }

    @Override
    public String columnType() {
        return "Type";
    }

    @Override
    public String columnComment() {
        return "Comment";
    }

    @Override
    public String columnKey() {
        return "Key";
    }

    public boolean isKeyIdentity(ResultSet results) throws SQLException {
        return "auto_increment".equals(results.getString("Extra"));
    }

    @Override
    public String isNullable() {
        return "Null";
    }

    @Override
    public String columnPosition() {
        return "Default";
    }

    @Override
    public String precision() {
        return "Default";
    }

    @Override
    public String scale() {
        return "Default";
    }

    @Override
    public String autoIncrement() {
        return "Default";
    }

    @Override
    public String isPK() {
        return "YES";
    }
}
